package com.cyl.XmlDemo.web.bysj;

import java.util.Iterator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Service;

import com.cyl.XmlDemo.entity.Room;
import com.cyl.XmlDemo.entity.StompClient;

//@Service
public class MsgService {
	
	private static final Logger logger = LoggerFactory.getLogger(MsgService.class);
	
	@Autowired
	private SimpMessagingTemplate messaging;
	
/*	@Autowired
	private StompClient stompClient;*/
	
	@Autowired
	private Room room;
	
	/**
	 * 传递ICE候选信息
	 * @param uniqueName 排除发送者
	 * @param msg 发送的信息
	 */
	public void relayICE(String uniqueName,String msg){
		Iterator<String> iterator = room.iterator();
		while(iterator.hasNext()){
			String uuid = iterator.next();
			if(!uuid.equals(uniqueName)){
				messaging.convertAndSendToUser(uuid, "/topic/private/message", msg);
			}
		}
		
	}
	
	public void reactCreateOrJoin(String user,String msg){
		messaging.convertAndSendToUser(user, "/topic/private/message", msg);
	}
	
	/**
	 * 创建规则：第一个人创建，剩下的人加入
	 * @param uniqueName
	 */
	public void reactCreateOrJoin(String uniqueName){
		Iterator<String> iterator = room.iterator();
		messaging.convertAndSendToUser(iterator.next(), "/topic/private/message", "create");
		while(iterator.hasNext()){
			messaging.convertAndSendToUser(iterator.next(), "/topic/private/message", "join");
		}
	}
}
